package com.itheima.controller;

import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParm;
import com.itheima.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/students")
public class StudentController {

    @Autowired
    private StudentService studentService;


    /**
     * 1 学生分页·查询
     */
    @GetMapping
    public Result Page(StudentQueryParm studentQueryParm) {
        log.info("分页查询,参数:{}", studentQueryParm);
        PageResult<Student> studentPageResult = studentService.page(studentQueryParm);
        return Result.success(studentPageResult);

    }

    /**
     * 2 新增学生
     */
    @PostMapping
    public Result add(@RequestBody Student student){
        log.info("新增学生数据:{}", student);
        studentService.add(student);
        return Result.success();
    }

    /**
     * 3 根据id查询学生
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable  Integer id){
        log.info("根据id查询学生:{}", id);
       Student student = studentService.findById(id);
        return Result.success(student);
    }

    /**
     * 4 修改学生
     */
    @PutMapping
    public Result update(@RequestBody Student student){
        log.info("修改学生数据:{}", student);
        studentService.update(student);
        return Result.success();
    }
    /**
     * 5 删除学生
     */
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        log.info("删除学生数据:{}", ids);
        studentService.delete(ids);
        return Result.success();
    }
    /**
     * 6 违纪处理
     */
    @PutMapping("/violation/{id}/{score}")
    public Result violation(@PathVariable Integer id,@PathVariable Integer score){
        log.info("违纪处理:{},{}", id, score);
        studentService.violation(id,score);
        return Result.success();
    }
}
